package lyl.itext;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;

public class ITextTest {
	private static BaseFont bfChinese;

	public static void main(String[] args) {
		try {
			bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    			
//			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
//					BaseFont.NOT_EMBEDDED);

			 createCover("市政府");
//			createIndex();
//			createContent();
			createZwgk();
//			test1();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void createZwgk() throws Exception{
		Paragraph title1 = new Paragraph("济南市政府",new Font(bfChinese, 20, Font.BOLD));
		title1.setKeepTogether(true);
		Chapter chapter1 = new Chapter(title1, 1);		
		Document document = new Document(PageSize.A4, 50, 50, 50, 50);
		PdfWriter.getInstance(document,
				new FileOutputStream("d:\\zwgk.pdf"));
		document.open();
		Section section1 = chapter1.addSection("封面");
		Section section2 = chapter1.addSection("目次");
		Section section3 = chapter1.addSection("正文");
		Paragraph cover = createCover("市政府");
		Paragraph index = createIndex();
		Paragraph content = createContent(document.getPageSize().getWidth());
//		content.setKeepTogether(true);
//		section1.add(content);
//		document.add(chapter1);
		document.add(cover);
		document.newPage();
		document.add(index);
		document.newPage();
		document.add(content);
		
		document.close();
	}

	public static Paragraph createCover(String siteName) {
		Paragraph coverPara = new Paragraph();
		coverPara.setAlignment("center");
		try {
			Font FontChinese = new Font(bfChinese, 25, Font.BOLD);

//			Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//			PdfWriter writer = PdfWriter.getInstance(document,
//					new FileOutputStream("d:\\cover.pdf"));
			// PdfContentByte cb = writer.getDirectContent();
//			document.open();
//			HeaderFooter header = new HeaderFooter(new Phrase(
//					"This is a header."), false);
//			HeaderFooter footer = new HeaderFooter(new Phrase("This is page "),
//					new Phrase("."));
//			document.setHeader(header);
//			document.setFooter(footer);
//			document.addAuthor("Bruno Lowagie");
//			document.addSubject("This is the result of a Test.");
			coverPara.add(getNewLine(7));
			Paragraph para1 = new Paragraph("济南市", FontChinese);
			para1.add(new Phrase(siteName, new Font(bfChinese, 25,
					Font.UNDERLINE)));
			para1.setMultipliedLeading(10);
			para1.setAlignment("center");
			coverPara.add(para1);
			Paragraph para2 = new Paragraph("\n政府信息公开目录", FontChinese);
			para2.setAlignment("center");
			coverPara.add(para2);
			coverPara.add(getNewLine(25));
			Paragraph para3 = new Paragraph(
					"编制部门：____________________\n编制时间：____________________",
					new Font(bfChinese, 12, Font.NORMAL));

			para3.setMultipliedLeading(2);
			para3.setAlignment("center");
			coverPara.add(para3);
//			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return coverPara;
	}

	public static Paragraph createIndex() {
		Paragraph indexPara = new Paragraph();
		indexPara.setAlignment("center");
		try {
			Font FontChinese = new Font(bfChinese, 25, Font.BOLD);

//			Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//			PdfWriter writer = PdfWriter.getInstance(document,
//					new FileOutputStream("d:\\index.pdf"));
//			document.open();
			indexPara.add(getNewLine(2));
			Paragraph para1 = new Paragraph("目      次", FontChinese);
			para1.setAlignment("center");
			indexPara.add(para1);
			indexPara.add(getNewLine(1));
			Paragraph para2 = new Paragraph(padding("1 机构职能", ".", 60) + 1,
					new Font(bfChinese, 12, Font.NORMAL));
			para2.setAlignment("right");
			Paragraph catePara = new Paragraph(
					padding("  1.1 职能简介", ".", 60) + 1,new Font(bfChinese, 12, Font.NORMAL));
			catePara.setAlignment("right");
			indexPara.setLeading(20);
			indexPara.add(para2);
			indexPara.add(catePara);

			indexPara.add(para2);
			indexPara.add(catePara);
			indexPara.add(para2);
			indexPara.add(catePara);
			indexPara.add(para2);
			indexPara.add(catePara);
//			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return indexPara;
	}
	public static Paragraph createContent(float pageWidth){
		Paragraph contentPara = new Paragraph();
		contentPara.setAlignment("center");
		try {
			Font FontChinese = new Font(bfChinese, 20, Font.BOLD);
			Font headerFont = new Font(bfChinese, 9, Font.BOLD);
			Font cellFont = new Font(bfChinese, 9, Font.NORMAL);

//			Document document = new Document(PageSize.A4, 20, 20, 20, 20);
//			PdfWriter writer = PdfWriter.getInstance(document,
//					new FileOutputStream("d:\\content.pdf"));
//			document.open();
			contentPara.add(getNewLine(1));
			Paragraph para1 = new Paragraph("2.1 地方性法规", FontChinese);
			para1.setAlignment("left");
			contentPara.add(para1);
			contentPara.add(getNewLine(1));
			
			Paragraph para2 = new Paragraph();
			para2.setAlignment(Element.ALIGN_CENTER);
			
			Table t = new Table(5, 2);
			t.setWidths(new int[]{20,20,38,12,10});
			t.setBorderColor(new Color(0, 0, 0));
			t.setPadding(2);
			t.setSpacing(0);
			t.setCellsFitPage(true);
			t.setLocked(true);
			t.setWidth(pageWidth-50);
			t.setBorderWidth(1);
			Cell c1 = new Cell(new Phrase("信息索引号", headerFont));
			c1.setHeader(true);
			c1.setBackgroundColor(new Color(220,220,220));
			c1.setHorizontalAlignment(Cell.ALIGN_CENTER);
			c1.setVerticalAlignment(Cell.ALIGN_MIDDLE);
			t.addCell(c1);
			c1 = new Cell(new Phrase("信息名称", headerFont));
			c1.setHeader(true);
			c1.setBackgroundColor(new Color(220,220,220));
			c1.setHorizontalAlignment(Cell.ALIGN_CENTER);
			c1.setVerticalAlignment(Cell.ALIGN_MIDDLE);
			t.addCell(c1);
			c1 = new Cell(new Phrase("内容描述", headerFont));
			c1.setHeader(true);
			c1.setBackgroundColor(new Color(220,220,220));
			c1.setHorizontalAlignment(Cell.ALIGN_CENTER);
			c1.setVerticalAlignment(Cell.ALIGN_MIDDLE);
			t.addCell(c1);
			c1 = new Cell(new Phrase("发布日期", headerFont));
			c1.setHeader(true);
			c1.setBackgroundColor(new Color(220,220,220));
			c1.setHorizontalAlignment(Cell.ALIGN_CENTER);
			c1.setVerticalAlignment(Cell.ALIGN_MIDDLE);
			t.addCell(c1);
			c1 = new Cell(new Phrase("公开类型", headerFont));
			c1.setHeader(true);
			c1.setBackgroundColor(new Color(220,220,220));
			c1.setHorizontalAlignment(Cell.ALIGN_CENTER);
			c1.setVerticalAlignment(Cell.ALIGN_MIDDLE);
			t.addCell(c1);
			t.endHeaders();
			t.addCell(new Phrase("3701-INFO-ucap-20080618-0032", cellFont));
			t.addCell(new Phrase("与高考改革同步 无分数评价掀起教育改革风暴", cellFont));
			t.addCell(new Phrase("2007年末，一股前所未有的教育改革风暴从山东刮起...", cellFont));
			t.addCell(new Phrase("2008-06-19", cellFont));
			t.addCell(new Phrase("主动公开", cellFont));
			para2.add(t);
			
			contentPara.add(para2);
			
//			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return contentPara;
	}
	
	private static String padding(String text, String str, int allLength) {
		while (text.length() < allLength)
			text += str;
		return text;
	}

	/**
	 * @param lines
	 * @return
	 */
	private static Paragraph getNewLine(int lines) {
		Paragraph newLine = new Paragraph();
		for (int i = 0; i < lines; i++) {
			newLine.add("\n");
		}
		return newLine;
	}

	public static void test2() {
		try {
			Document document = new Document(PageSize.A4, 50, 50, 50, 50);
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream("d:\\ITextTest.pdf"));
			document.open();
			document.add(new Paragraph("First page of the document."));
			document
					.add(new Paragraph(
							"Some more text on the first page with different color and font type.",
							FontFactory.getFont(FontFactory.COURIER, 14,
									Font.BOLD, new Color(255, 150, 200))));

			Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(
					FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0,
							255)));
			Chapter chapter1 = new Chapter(title1, 1);
			chapter1.setNumberDepth(0);
			Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
					FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
							new Color(255, 0, 0)));
			Section section1 = chapter1.addSection(title11);

			Table t = new Table(3, 2);
			t.setBorderColor(new Color(220, 255, 100));
			t.setPadding(5);
			t.setSpacing(5);
			t.setBorderWidth(1);
			Cell c1 = new Cell("header1");
			c1.setHeader(true);
			t.addCell(c1);
			c1 = new Cell("Header2");
			t.addCell(c1);
			c1 = new Cell("Header3");
			t.addCell(c1);
			t.endHeaders();
			t.addCell("1.1");
			t.addCell("1.2");
			t.addCell("1.3");
			section1.add(t);

			List l = new List(true, false, 10);
			l.add(new ListItem("First item of list"));
			l.add(new ListItem("Second item of list"));
			section1.add(l);

			document.add(chapter1);
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void test1() {
		try {
			Document document = new Document(PageSize.A4, 50, 50, 50, 50);
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream("d:\\ITextTest.pdf"));
			document.open();
			document.add(new Paragraph("First page of the document."));
			document
					.add(new Paragraph(
							"Some more text on the first page with different color and font type.",
							FontFactory.getFont(FontFactory.COURIER, 14,
									Font.BOLD, new Color(255, 150, 200))));

			Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(
					FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0,
							255)));
			Chapter chapter1 = new Chapter(title1, 1);
			chapter1.setNumberDepth(0);
			Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
					FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
							new Color(255, 0, 0)));
			Section section1 = chapter1.addSection(title11);
			Paragraph someSectionText = new Paragraph(
					"This text comes as part of section 1 of chapter 1.");
			section1.add(someSectionText);
			someSectionText = new Paragraph("Following is a 3 X 2 table.");
			section1.add(someSectionText);
			Table t = new Table(3, 2);
			t.setBorderColor(new Color(220, 255, 100));
			t.setPadding(5);
			t.setSpacing(5);
			t.setBorderWidth(1);
			Cell c1 = new Cell("header1");
			t.addCell(c1);
			c1 = new Cell("Header2");
			t.addCell(c1);
			c1 = new Cell("Header3");
			t.addCell(c1);
			t.addCell("1.1");
			t.addCell("1.2");
			t.addCell("1.3");
			section1.add(t);

			List l = new List(true, true, 10);
			l.add(new ListItem("First item of list"));
			l.add(new ListItem("Second item of list"));
			section1.add(l);

			document.add(chapter1);
			document.close();
		} catch (Exception e2) {
			System.out.println(e2.getMessage());
		}
	}
}
